
/* --------------------------------------
 * Copyright 2008 Jean-Matthieu BARBIER
 * --------------------------------------
 * This file is part of QLocatePC.
 *
 * QLocatePC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QLocatePC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QLocatePC.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "setupwin.h"
#include "httplocate.h"
#include <QtGui>
#include <QtNetwork>
//
QLocatePCSetupWin::QLocatePCSetupWin( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f)
{
  setupUi(this);
  errorDialog = new QErrorMessage(this);
  infoDialog = new QMessageBox(this);
  loc = new QLocatePCHttpLocate(this);
  connect(btnSetup,SIGNAL(clicked()),this,SLOT(actionApplyParams()));
  connect(btnSetDefaults,SIGNAL(clicked()),this,SLOT(actionSetDefaults()));
  connect(loc,SIGNAL(locSuccess(QString)),errorDialog,SLOT(showMessage(QString)));
  connect(loc,SIGNAL(locFailed(QString)),errorDialog,SLOT(showMessage(QString)));
  connect(this,SIGNAL(updateDaemon()),loc,SLOT(updateDaemon()));
  connect(spbTimerInterval,SIGNAL(valueChanged(int)),this,SLOT(actionSetupDaemon()));
  connect(cbbAutoLoc,SIGNAL(stateChanged(int)),this,SLOT(actionSetupDaemon()));
  connect(cbbStartup,SIGNAL(stateChanged(int)),this,SLOT(actionSetupStartup()));
}

void QLocatePCSetupWin::actionApplyParams() {
  QLocatePCSetupData setup;
  setupFromForm(&setup);
}

void QLocatePCSetupWin::actionSetDefaults() {
  QLocatePCSetupData setup;
  setup.load();
  setup.setdefaults();
  updateFromSetup(&setup);  
}

void QLocatePCSetupWin::actionSetupDaemon() {
  QLocatePCSetupData setup;
  setup.load();
  setup.daemon = cbbAutoLoc->isChecked();
  setup.daemoninterval = spbTimerInterval->value()*1000*60;
  setup.set();
  emit updateDaemon();
}

void QLocatePCSetupWin::actionSetupStartup() {
  if(QLocatePCSetupData::loadatstartup(cbbStartup->isChecked(),true)) {
    errorDialog->showMessage("Lancement automatique actif");
  } else {
    errorDialog->showMessage("Lancement automatique non actif");
  }
}

void QLocatePCSetupWin::setupFromForm(QLocatePCSetupData* setup) {
  setup->url = edtSetupURL->text();
  setup->computername = edtComputerName->text();
  setup->identifier = edtPCIdentifier->text();
  setup->moreinfo = edtMoreInfo->toPlainText();
  QUrl url;
  loc->makeUrl(setup, &url,QLocatePCHttpLocate::actionSetup);
  if(!url.isValid()) {
    errorDialog->showMessage("Adresse invalide");
  } else {
    loc->goLocate(setup,QLocatePCHttpLocate::actionSetup);
    setup->set();
  }
}

void QLocatePCSetupWin::updateFromSetup(QLocatePCSetupData* setup) {
  edtSetupURL->setText(setup->url);
  edtComputerName->setText(setup->computername);
  edtPCIdentifier->setText(setup->identifier);
  edtMoreInfo->setPlainText(setup->moreinfo);
  spbTimerInterval->setValue(setup->daemoninterval/60000);
  cbbAutoLoc->setChecked(setup->daemon);
  cbbStartup->setChecked(setup->loadatstartup(false,false));
}
//
